/**
 * Created by Administrator on 2017/3/18.
 */
var U_flag = false;
var P_flag = false;
var N_flag = false;
var P2_flag = false;
function loadweb() {
    //判断用户是否有登陆记录（session 不为空）
    $.get("","isemtry = 1" ,function (msg) {
        if (msg == "notempty"){
            redirect("../Mainpage/mainpage");
        }
    });
    var randomNum = Math.random() * 5;
    var i = parseInt(randomNum, 10);
    document.body.style.background = "url('/Chat_project/web/Public/img/1.jpg')";

}
//得到 getCookie
function CheckCookie() {
    $.ajaxSetup({
        async : false
    });
   $.get("","isemtry = 1",function (msg) {
       window.alert(msg);
       if(msg == "notempty"){
           return "true";
       }else if(msg == "empty"){
           return "false";
       }
   });
}
//Ajax数据请求
function sendDate() {
    // window.alert($('#form').serialize());
    $.post("", $('#form').serialize(), function (msg) {
        if (msg == "login_success") {
            showWarm("登陆成功");
            // return;
        } else if (msg == "login_failure") {
            showWarm("登陆失败");
            return;
        } else if (msg == "register_success") {
            showWarm("注册成功");
            // return;
        } else if (msg == "register_failure") {
            showWarm("注册失败");
            return;
        } else if (msg == "exits") {
            showWarm("用户名已存在");
            return;
        }
        // showWarm(msg);
        redirect("../Mainpage/mainpage");
        // window.location.href = "../Mainpage/mainpage";
    });
}
//是否长时间记住密码
function ischeck() {
    var remember = document.getElementById("remember");
    var date_time = document.getElementById("date");
    if (remember.checked) {
        date_time.value = "yes";
        console.log("yes");
    } else {
        date_time.value = "no";
        console.log("no");
    }
}
//用来跳转页面
function redirect(web) {
    window.location.href = web;
}
//判断是否发送数据到 服务器数据库
function isSend(which) {

    if (which.length - 4 == 2) {
        if (U_flag && P_flag) {
            sendDate();
            return false;
        }
    } else if (which.length - 2 == 3) {
        if (U_flag && P_flag && P2_flag) {
            sendDate();
            return false;
        }
    }
    showWarm("输入错误");
    return false;
}
//用来提示用户输入情况（以提示框的形式提示）
function showWarm(world) {
    if (world != null && world.trim() != "") {
        var prompt = document.getElementById("prompt_body");
        prompt.innerHTML = "<p>" + world + "</p>";
        $('#prompt').modal();
    }
}
//保存第一次密码
var first_password = "";
var is_same = false;
//校验2次密码
function samepassword(first, second) {
    if (first == second) {
        return true;
    } else {
        return false;
    }
}

//检查用户输入信息
function checkuser(which) {
    var check_name = "";
    //判断输出
    var out_world = "";
    check_name = which.name;
    var user_which = document.getElementById(check_name);
    if (which.name == "username") {

        out_world = "用户名";
    }

    else if (which.name == "password") {
        first_password = user_which.value.trim();
        out_world = "密码";
    } else if (which.name == "nickname") {
        out_world = "昵称";
    } else if (which.name == "password2") {
        out_world = "重新输入的密码";
        if (samepassword(first_password, user_which.value.trim())) {
            is_same = false;
        } else {
            is_same = true;
        }
    }
    //得到用户名或者密码的id

    var user = user_which.value.trim();
    var lable_name = check_name + "_label";
    //给用户提示 是否输入正常
    var span_name = check_name + "_span";
    var span = document.getElementById(span_name);
    var lable = document.getElementById(lable_name);
    if (user == "") {
        // span.setAttribute("class", " glyphicon glyphicon-remove form-control-feedback");
        lable.innerHTML = out_world + "输入不能为空";
        // lable.setAttribute("class", "control-label");
    } else if (is_same) {
        lable.innerHTML = out_world + "两次密码输入不对";
        is_same = false;
    } else if (CheckLogin(user) == "notstyle" && which.name != "nickname") {
        // span.setAttribute("class", " glyphicon glyphicon-remove form-control-feedback");
        lable.innerHTML = out_world + "格式不正确";
        // lable.setAttribute("class", "control-label");
    }
    else if (CheckLogin(user) == "notchange" && which.name != "nickname") {

        lable.innerHTML = out_world + "4-18个字母或数字";
        // span.setAttribute("class", " glyphicon glyphicon-remove form-control-feedback");
        // lable.setAttribute("class", "control-label");
    } else if (CheckLogin(user) == "ch" && which.name == "nickname") {
        lable.innerHTML = out_world + " 只能输入文字或者字母";
    } else {
        if (which.name == "username") {
            U_flag = true;
        } else if (which.name == "password") {
            P_flag = true;
        } else if (which.name == "nickname") {
            N_flag = true;
        } else if (which.name == "password2") {
            P2_flag = true;
        }
        span.setAttribute("class", "glyphicon glyphicon-ok form-control-feedback");
        lable.innerHTML = "";
        return;
    }
    if (which.name == "username") {
        U_flag = false;
    } else if (which.name == "password") {
        P_flag = false;
    } else if (which.name == "nickname") {
        N_flag = false;
    } else if (which.name == "password2") {
        P2_flag = false;
    }
    span.setAttribute("class", " glyphicon glyphicon-remove form-control-feedback");
    lable.setAttribute("class", "control-label");
}
//检查输入合法性
function CheckLogin(number) {
    var p = /^[0-9a-zA-Z]*$/g;
    // var p = /[0-9a-zA-Z]/;
    var length = /[0-9a-zA-Z]{4,18}/;
    // var p = /[0-9a-zA-Z]{4,23}/;
    //判断输入格式
    var ch = /^[a-zA-Z\u4e00-\u9fa5]*$/g;
    if (!p.test(number)) {
        return "notstyle";
    }
    //判断输入长度
    else if (!length.test(number)) {
        return "notchange";
    } else if (!ch.test(number)) {
        return "ch";
    }
    else {
        return "true";
    }

}